layui.use(['layer', 'form', 'admin', 'laydate', 'ax'], function () {
    var $ = layui.jquery;
    var $ax = layui.ax;
    var form = layui.form;
    var admin = layui.admin;
    var laydate = layui.laydate;
    var layer = layui.layer;

    // 让当前iframe弹层高度适应
    admin.iframeAuto();

    // 添加表单验证方法
    form.verify({
        psw: [/^[\S]{6,12}$/, '密码必须6到12位，且不能出现空格'],
        repsw: function (value) {
            if (value !== $('#regForm input[name=password]').val()) {
                return '两次密码输入不一致';
            }
        }
    });

    // 表单提交事件
    form.on('submit(btnSubmit)', function (data) {
        var ajax = new $ax(Feng.ctxPath + "/openApi/register", function (data) {
            Feng.success("注册成功！");
            //关掉对话框
            admin.closeThisDialog();
        }, function (data) {
            Feng.error("注册失败！"+ data.responseJSON.message)
        });
        ajax.set(data.field);
        ajax.start();
    });
    $("#reg").click(function () {
        layer.open({
            type: 2,
            title : '新用户注册',
            area: ['330px', '400px'], //宽高
            shadeClose: true, //开启遮罩关闭
            content: "/openApi/reg"
        });
    });
    $("#retrieve").click(function () {
        $(".login-body").attr("hidden",true);
        $("#msform").attr("hidden",false);
    });
    $("#cancel").click(function () {
        $(".login-body").attr("hidden",false);
        $("#msform").attr("hidden",true);
    })
    $("#return").click(function () {
        $(".login-body").attr("hidden",false);
        $("#msform").attr("hidden",true);
    })

    var wait=300;
    function time(o) {
        if (wait == 0) {
            o.removeAttribute("disabled");
            o.value="获取邮箱验证码";
            wait = 300;
        } else {
            o.setAttribute("disabled", true);
            o.value="重新发送(" + wait + ")";
            wait--;
            setTimeout(function() {time(o)}, 1000)
        }
    }
    $("#retrievePassword").click(function () {
        time(this);
        var email = $("#email").val();
        if (email === ""){
            Feng.error("邮箱地址获取失败")
        } else {
            var index = layer.load(1, {
                shade: [0.1,'#fff'] //0.1透明度的白色背景
            });
            $.ajax({
                url : "/email/vf/retrievePassword",
                data : {
                    email : $("#email").val()
                },
                dataType : "json",
                success : function (data) {
                    var json = JSON.parse(data);
                    if (json.code === 0){
                        Feng.success(json.msg)
                    } else {
                        Feng.error(json.msg)
                    }
                    layer.close(index);
                },error : function () {
                    Feng.error("网络错误，获取失败，请刷新页面后重试")
                    layer.close(index);
                }
            });
        }
    });
    
    var current_fs, next_fs, previous_fs; //fieldsets
    var left, opacity, scale; //fieldset properties which we will animate
    var animating; //flag to prevent quick multi-click glitches

    $(".next").click(function(){
        var myreg = /^([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,3}$/;
        var email = $("#email").val();
        var falg = false;
        if (email === ""){
            Feng.error("请输入邮箱")
        } else if(!myreg.test(email)){
            Feng.error("邮件格式不正确")
        } else {
            $.ajax({
               url : "/email/vf/emailVerification",
               data : {
                   email : email
               },
               async : false,
               success : function (data) {
                    if (data === "success" ){
                        falg = true;
                    } else {
                        Feng.error("没有此邮箱，请重新输入")
                    }
               } , error : function () {
                   Feng.error("网络异常，校验失败")
               }
            });
            if (falg){
                if(animating) return false;
                animating = true;
                current_fs = $(this).parent();
                next_fs = $(this).parent().next();
                //activate next step on progressbar using the index of next_fs
                $("#progressbar li").eq($("fieldset").index(next_fs)).addClass("active");

                //show the next fieldset
                next_fs.show();
                //hide the current fieldset with style
                current_fs.animate({opacity: 0}, {
                    step: function(now, mx) {
                        //as the opacity of current_fs reduces to 0 - stored in "now"
                        //1. scale current_fs down to 80%
                        scale = 1 - (1 - now) * 0.2;
                        //2. bring next_fs from the right(50%)
                        left = (now * 50)+"%";
                        //3. increase opacity of next_fs to 1 as it moves in
                        opacity = 1 - now;
                        current_fs.css({'transform': 'scale('+scale+')'});
                        next_fs.css({'left': left, 'opacity': opacity});
                    },
                    duration: 800,
                    complete: function(){
                        current_fs.hide();
                        animating = false;
                    }
                });
            }
        }
    });

    $(".nextTo").click(function(){
        var falg = false;
        var email = $("#email").val();
        var emailCode = $("#emailCode").val();
        if (emailCode === ""){
            Feng.error("请输入验证码")
        } else if (email === "") {
            Feng.error("请输入邮箱")
        } else {
            var index = layer.load(1, {
                shade: [0.1,'#fff'] //0.1透明度的白色背景
            });
            $.ajax({
                url : "/email/vf/isCheckEmailCode",
                data : {
                    email : email,
                    emailCode : emailCode,
                    type : 1
                },
                async : false,
                dataType : "json",
                success : function (data) {
                    var json = JSON.parse(data);
                    if (json.code === 0){
                        falg = true;
                    } else {
                        Feng.error(json.msg)
                    }
                    layer.close(index);
                },error : function () {
                    Feng.error("网络异常，校验失败")
                    layer.close(index);
                }
            });
            if (falg){
                if(animating) return false;
                animating = true;
                current_fs = $(this).parent();
                next_fs = $(this).parent().next();
                //activate next step on progressbar using the index of next_fs
                $("#progressbar li").eq($("fieldset").index(next_fs)).addClass("active");

                //show the next fieldset
                next_fs.show();
                //hide the current fieldset with style
                current_fs.animate({opacity: 0}, {
                    step: function(now, mx) {
                        //as the opacity of current_fs reduces to 0 - stored in "now"
                        //1. scale current_fs down to 80%
                        scale = 1 - (1 - now) * 0.2;
                        //2. bring next_fs from the right(50%)
                        left = (now * 50)+"%";
                        //3. increase opacity of next_fs to 1 as it moves in
                        opacity = 1 - now;
                        current_fs.css({'transform': 'scale('+scale+')'});
                        next_fs.css({'left': left, 'opacity': opacity});
                    },
                    duration: 800,
                    complete: function(){
                        current_fs.hide();
                        animating = false;
                    }
                });
            }
        }
    });

    $(".previous").click(function(){
        if(animating) return false;
        animating = true;

        current_fs = $(this).parent();
        previous_fs = $(this).parent().prev();

        //de-activate current step on progressbar
        $("#progressbar li").eq($("fieldset").index(current_fs)).removeClass("active");

        //show the previous fieldset
        previous_fs.show();
        //hide the current fieldset with style
        current_fs.animate({opacity: 0}, {
            step: function(now, mx) {
                //as the opacity of current_fs reduces to 0 - stored in "now"
                //1. scale previous_fs from 80% to 100%
                scale = 0.8 + (1 - now) * 0.2;
                //2. take current_fs to the right(50%) - from 0%
                left = ((1-now) * 50)+"%";
                //3. increase opacity of previous_fs to 1 as it moves in
                opacity = 1 - now;
                current_fs.css({'left': left});
                previous_fs.css({'transform': 'scale('+scale+')', 'opacity': opacity});
            },
            duration: 800,
            complete: function(){
                current_fs.hide();
                animating = false;
            }
        });
    });

});
